export default {
    name: 'setOrder',
    data() {
        return {
            user: {},
            multipleSelection: [],
            addressList: [],
            showList: [],
            checkAddress: ''
        }
    },
    created() {
        this.axios.defaults.headers.common['Authorization'] = localStorage['token'];
        this.axios.get('api/getAddressInfo', { params: { uesrId: localStorage.getItem('currentUser').split('id')[1].split('"')[2] } })
            .then(res => {
                this.addressList = res.data.data;
                for (let i = 0; i < this.addressList.length; i++) {
                    let obj = {
                        value: '地址：' + String(this.addressList[i].address_details) +
                            '  收件人：' + String(this.addressList[i].address_receive) +
                            '  手机号：' + String(this.addressList[i].address_phone),
                        label: '地址：' + String(this.addressList[i].address_details) +
                            '  收件人：' + String(this.addressList[i].address_receive) +
                            '  手机号：' + String(this.addressList[i].address_phone)
                    }
                    this.showList.push(obj)
                }
                console.log('addressList: ', this.addressList)
                console.log('showList: ', this.showList)
            })
        this.multipleSelection = JSON.parse(this.$route.query.data)
        console.log('this.multipleSelection', this.multipleSelection)
        this.user = JSON.parse(localStorage.getItem('currentUser'))
    },
    methods: {
        addOrder() {
            //获取当前订单的总价
            let totalPrice = this.TotalPrice
            let checkAddress = this.checkAddress
            if (this.checkAddress.length<1) {
                this.$message({
                    type: 'error',
                    message: '请选择收获地址!',
                    duration: 1000
                });
                return
            }
            //获取所有下单商品的购物车编号id
            let ids = this.multipleSelection.map(item => {
                return item._id
            })
            //获取所有下单商品的具体内容
            let products = this.multipleSelection.map(item => {
                return item.product
            })
            for (let i = 0; i < products.length; i++) {
                products[i].num = this.multipleSelection[i].num
            }
            console.log('products123', products)
            this.axios.defaults.headers.common['Authorization'] = localStorage['token']
            this.axios.post('api/addOrder',
                { order: { products, totalPrice, checkAddress } })
                .then(res => {
                    //获取刚刚提交的订单编号
                    let orderId = res.data.data
                    let userLike = res.data.message
                    localStorage.setItem('userLike', userLike)
                    this.$message({
                        type: 'success',
                        duration: 1000,
                        message: '订单提交成功'
                    });
                    this.axios.delete('api/deleteCarts', { params: { ids } })
                        .then(res => {
                            console.log('购物车记录已清除')
                            this.$router.push({ path: '/index/pay', query: { data: totalPrice, orderId: orderId } })
                        })
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        duration: 1000,
                        message: '订单提交失败'
                    });
                })
        }
    },
    computed: {
        TotalPrice() {
            let total = 0
            this.multipleSelection.map(item => {
                total += parseInt(item.product.price) * parseInt(item.num)
            })
            return total
        }
    }
}